package org.caixy.project.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.caixy.project.admin.dto.req.UserLoginReqDTO;
import org.caixy.project.admin.dto.resp.UserRespDTO;
import org.caixy.project.admin.service.UserService;
import org.caixy.project.admin.common.convention.exception.ClientException;
import org.caixy.project.admin.common.enums.UserCodeError;
import org.caixy.project.admin.dao.entity.UserDO;
import org.caixy.project.admin.dao.mapper.UserMapper;
import org.caixy.project.admin.dto.req.UserUpdateReqDTO;
import org.caixy.project.admin.dto.resp.UserLoginRespDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserService {

    @Override
    public UserRespDTO getUserByUserName(String username) {
        LambdaQueryWrapper<UserDO> queryMapper = Wrappers.lambdaQuery(UserDO.class).eq(UserDO::getUsername, username);
        UserDO userDo = baseMapper.selectOne(queryMapper);
        if(userDo == null){
            throw new ClientException(UserCodeError.USER_NULL);
        }
        UserRespDTO result = new UserRespDTO();
        BeanUtils.copyProperties(userDo,result);
        return result;
    }

    @Override
    public Boolean hasUserName(String username) {
        LambdaQueryWrapper<UserDO> queryMapper = Wrappers.lambdaQuery(UserDO.class).eq(UserDO::getUsername, username);
        UserDO userDO = baseMapper.selectOne(queryMapper);
        return userDO == null;
    }

    @Override
    public void Register(UserUpdateReqDTO requestParam) {
        if(!hasUserName(requestParam.getUsername())){
            throw new ClientException(UserCodeError.USER_EXIST);
        }
        int inserted = baseMapper.insert(BeanUtil.toBean(requestParam, UserDO.class));
        if(inserted<1){
            throw new ClientException(UserCodeError.USER_SAVE_ERROR);
        }
    }

    @Override
    public void Update(UserUpdateReqDTO requestParam) {
        LambdaUpdateWrapper<UserDO> updateMapper = Wrappers.lambdaUpdate(UserDO.class).eq(UserDO::getUsername, requestParam.getUsername());
        int result = baseMapper.update(BeanUtil.toBean(requestParam, UserDO.class),updateMapper);

    }

    @Override
    public UserLoginRespDTO Login(UserLoginReqDTO requestParam) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
                .eq(UserDO::getUsername, requestParam.getUsername())
                .eq(UserDO::getPsw, requestParam.getPsw())
                .eq(UserDO::getDelFlag, 0);
        UserDO userDO = baseMapper.selectOne(queryWrapper);
        if (userDO == null) {
            throw new ClientException("用户不存在");
        }
        String uuid = UUID.randomUUID().toString();
        return new UserLoginRespDTO(uuid);
    }
    @Override
    public void Logout(String username) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class)
                .eq(UserDO::getUsername,username);
        int delete = baseMapper.delete(queryWrapper);
    }
}
